package com.es.disped.web.controller.clazz;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.es.disped.api.clazz.ClazzService;
import com.es.disped.common.constant.RecordStatus;
import com.es.disped.common.model.MsgModel;
import com.es.disped.common.model.PageModel;
import com.es.disped.core.controller.BaseController;
import com.es.disped.dao.model.Clazz;
import com.es.disped.dao.model.ClazzExample;
import com.es.disped.dao.model.SyllabusForeignModel;
import com.es.disped.dao.model.UserForeignModel;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author Anson<br>
 *	Copyright by EasyShare 2019<br>
 *  
 *  All right reserved<br>
 *
 *  Created on 2019年7月6日 上午10:08:09<br>
 *  名称：ClazzController.java<br>
 *  描述：班级管理<br>
 */
@Api(value="班级管理")
@Controller
@RequestMapping("/manage/clazz")
public class ClazzController extends BaseController{
	
	private final String USER_NAMESPACE="com.es.disped.dao.mapper.user.UserCustomMapper";
	
	private final String CLAZZ_NAMESPACE="com.es.disped.dao.mapper.clazz.ClazzCustomMapper";
	
	private final String SYLLABUS_NAMESPACE="com.es.disped.dao.mapper.syllabus.SyllabusCustomMapper";
	
	@Autowired
	ClazzService clazzService;

	
	@ApiOperation(value="保存班级",response=MsgModel.class,httpMethod="POST",notes="保存班级信息")
	@RequiresPermissions(value={"admin:save","sys:save"},logical=Logical.OR)
	@Transactional
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public @ResponseBody MsgModel save(@RequestBody @ApiParam(name="clazz",value="班级对象",required=true) Clazz clazz)
	{
		int count=0;
		if(Optional.of(clazz)
				.filter(name->clazz.getClazzName()!=null)
				.filter(tId->clazz.getTeacherId()!=null)
				.filter(max->clazz.getClazzMaxStudent()!=null)
				.filter(syId->clazz.getSyllabusId()!=null)
				.isPresent())
		{
			clazz.setClazzId(getUUID());
			
			UserForeignModel teacherForeignModel=this.getService().selectOne(
					USER_NAMESPACE+".getUserForeignModel", clazz.getTeacherId());
			clazz.setTeacherName(teacherForeignModel.getUserName());
			
			SyllabusForeignModel syllabusForeignModel=this.getService().selectOne(
					SYLLABUS_NAMESPACE+".getSyllabusForeignModel", clazz.getSyllabusId());
			clazz.setSyllabusName(syllabusForeignModel.getSyllabusName());
			
			if(!this.isNull(clazz.getAssistantId()))
			{
				UserForeignModel assistantForeignModel=this.getService().selectOne(USER_NAMESPACE+".getUserForeignModel", clazz.getAssistantId());
				clazz.setAssistantName(assistantForeignModel.getUserName());
			}
			
			clazz.setClazzValid(RecordStatus.USEAGE.getValue());
			System.out.println(">>>>>>>>>>>>>>>>>>>>>>"+clazz);
			count=clazzService.insertSelective(clazz);
		}
		return this.resultMsg(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"保存成功":"保存失败",count==1?clazz:"");
	}
	
	
	@ApiOperation(value="更新班级",response=MsgModel.class,httpMethod="POST",notes="更新班级信息")
	@RequiresPermissions(value={"admin:update","sys:update"},logical=Logical.OR)
	@Transactional
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public @ResponseBody MsgModel update(@RequestBody @ApiParam(name="clazz",value="班级对象",required=true) Clazz clazz)
	{
		int count=0;
		if(Optional.of(clazz)
				.filter(cId->clazz.getClazzId()!=null)
				.isPresent())
		{
			if(!this.isNull(clazz.getTeacherId()))
			{
				UserForeignModel teacherForeignModel=this.getService().selectOne(USER_NAMESPACE+".getUserForeignModel", clazz.getTeacherId());
				clazz.setTeacherName(teacherForeignModel.getUserName());
			}
			if(!this.isNull(clazz.getAssistantId()))
			{
				UserForeignModel assistantForeignModel=this.getService().selectOne(USER_NAMESPACE+".getUserForeignModel", clazz.getAssistantId());
				clazz.setAssistantName(assistantForeignModel.getUserName());
			}
			if(!this.isNull(clazz.getSyllabusId()))
			{
				SyllabusForeignModel syllabusForeignModel=this.getService().selectOne(
						SYLLABUS_NAMESPACE, clazz.getSyllabusId());
				clazz.setSyllabusName(syllabusForeignModel.getSyllabusName());
			}
			count=clazzService.updateByPrimaryKeySelective(clazz);
		}
		return this.resultMsg(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"保存成功":"保存失败",count==1?clazz:"");
	}
	
	@ApiOperation(value="删除班级",response=MsgModel.class,httpMethod="POST",notes="删除班级信息")
	@RequiresPermissions(value={"admin:delete","sys:delete"},logical=Logical.OR)
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public @ResponseBody MsgModel delete(@RequestBody @ApiParam(name="clazz",value="班级对象",required=true) Clazz clazz)
	{
		int count=0;
		if(!this.isNull(clazz.getClazzId()))
		{
			count=this.getService().delete(CLAZZ_NAMESPACE+".deleteClazzModel", clazz.getClazzId());
		}
		return this.resultMsg(count>=1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count>=1?"保存成功":"保存失败",count>=1?clazz:"");
	}
	
	
	@ApiOperation(value="检索班级",response=MsgModel.class,httpMethod="POST",
			notes="检索班级对象，精确检索：班级ID，教学计划ID，教师ID，助教ID；模糊检索：班级名称，教学大纲名称，教师姓名，助教姓名，班级最大容量")
	@RequestMapping(value="/list",method=RequestMethod.POST)
	public @ResponseBody MsgModel list(@RequestBody @ApiParam(name="clazz",value="班级对象",required=false) Clazz clazz)
	{
		ClazzExample listExample=new ClazzExample();
		ClazzExample.Criteria criteria=listExample.createCriteria();
		if(!this.isNull(clazz.getClazzId()))
		{
			criteria.andClazzIdEqualTo(clazz.getClazzId());
		}
		if(!this.isNull(clazz.getTeacherId()))
		{
			criteria.andTeacherIdEqualTo(clazz.getTeacherId());
		}
		if(!this.isNull(clazz.getAssistantId()))
		{
			criteria.andAssistantIdEqualTo(clazz.getAssistantId());
		}
		if(!this.isNull(clazz.getSyllabusId()))
		{
			criteria.andSyllabusIdEqualTo(clazz.getSyllabusId());
		}
		if(!this.isNull(clazz.getClazzName()))
		{
			criteria.andClazzNameLike("%"+clazz.getClazzName()+"%");
		}
		if(!this.isNull(clazz.getTeacherName()))
		{
			criteria.andTeacherNameLike("%"+clazz.getTeacherName()+"%");
		}
		if(!this.isNull(clazz.getAssistantName()))
		{
			criteria.andAssistantNameLike("%"+clazz.getAssistantName()+"%");
		}
		if(!this.isNull(clazz.getSyllabusName()))
		{
			criteria.andSyllabusNameLike("%"+clazz.getSyllabusName()+"%");
		}
		if(!this.isNull(clazz.getClazzMaxStudent()))
		{
			criteria.andClazzMaxStudentLessThanOrEqualTo(clazz.getClazzMaxStudent());
		}
		List<Clazz> list=clazzService.selectByExampleWithBLOBs(listExample);
		return this.resultMsg(!this.isNull(list)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(list)?"课时列表加载成功":"课时列表加载失败",!this.isNull(list)?list:"");
	}
	
	
	@ApiOperation(value="分页加载班级",response=MsgModel.class,httpMethod="POST",notes="分页加载班级，分页参数['offset','limit']")
	@RequestMapping(value="/pages",method=RequestMethod.POST)
	public @ResponseBody MsgModel pages(@RequestBody @ApiParam(name="paramMap",value="分页参数[offset,limit]",required=true) Map<String, Object> paramMap)
	{
		ClazzExample listExample=new ClazzExample();
		PageModel<Clazz> pageModel=null;
		if(Optional.of(paramMap)
				.filter(offset->paramMap.containsKey("offset")!=false)
				.filter(limit->paramMap.containsKey("limit")!=false)
				.isPresent())
		{
			pageModel=clazzService.selectByExampleWithBLOBsForOffsetPage(listExample,
					Integer.valueOf(paramMap.get("offset").toString()), Integer.valueOf(paramMap.get("limit").toString()));
			
		}
		return this.resultMsg(!this.isNull(pageModel)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(pageModel)?"课时加载成功":"课时加载失败",!this.isNull(pageModel)?pageModel:"");
	}
}
